package cn.yuhh.serviec.impl;

import cn.yuhh.dao.TagDao;
import cn.yuhh.entity.Tag;
import cn.yuhh.serviec.ITagService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TagService implements ITagService {

    @Autowired
    private TagDao tagDao;


    @Override
    public Page<Tag> listTag(Pageable pageable) {
        return tagDao.findAll(pageable);
    }

    @Override
    public Tag findById(Long id) {
        return tagDao.getOne(id);
    }

    @Override
    public Tag findByName(String name) {
        return tagDao.findByName(name);
    }

    @Override
    public void updateTagById(Long id, Tag tag) {
        Tag tag1 = tagDao.getOne(id);
        BeanUtils.copyProperties(tag, tag1);
        tagDao.save(tag1);
    }


    @Override
    public Tag save(Tag tag) {
        return tagDao.save(tag);
    }

    @Override
    public void deleteById(Long id) {
        tagDao.deleteById(id);
    }

    @Override
    public List<Tag> listTag() {
        return tagDao.findAll();
    }

    @Override
    public List<Tag> listTag(String tagIds){
        String[] split = tagIds.split(",");
        List<Long> ids = new ArrayList<>();
        for (String s : split) {
            System.out.println(s);
            ids.add(new Long(s));
        }
        return tagDao.findAllById(ids);
    }


}
